<html>
<head>
<title>IYOCGwP Book 1 - Chapter 6 - Hangman</title>
<link rel="stylesheet" href="inventbook.css" type="text/css" media="all">
</head>
<body>

<h1>Chapter 6 - Hangman</h1>


<!-- Conception
<P>TODO: This chapter was written, and then radically reorganized. I need to proof-read this chapter to make sure it all makes sense.</p>

Choose a secret word

Show the board & blanks
Ask the player
If player correct, remove blank.
	Check for win.
If player not, add a new body part.
	Check for game over.
If game over, ask to play again.

-->
<p>This game introduces many new concepts. But don't worry, we'll experiment with these programming concepts in the interactive shell first. Once you understand these concepts, it will be much easier to understand the game in this chapter: Hangman.</p>

<p>In case you don't know, Hangman is a game two people play with paper and pencil. One person thinks of a word, and then draws blanks for each letter in the secret word. The other person guesses letters that might be in the word. If they guess correctly, the first person writes the letter into the blank. If they guess wrong, the first person draws another body part of the hangman. If the second person can guess all the letters in the word before the hangman has completely been drawn, they win.</p>

<h2>ASCII Art</h2>

<p>This code is about four times larger than our Dragon World game! But don't worry. Half of the lines of code aren't really code at all, but are strings that use keyboard characters to draw pictures. This type of graphics is called <span class='term'>ASCII art</span> (pronounced "ask-ee"), because keyboard characters (such as letters, numbers, and also all the other signs on the keyboard) are called ASCII characters. ASCII stands for American Standard Code for Information Interchange. Here are some cats (with the artist's initials) done in ASCII art:</p>

<!-- TODO: might have to add newlines here so the cats don't get split across page breaks when we convert this to PDF -->
<table border="0" align="center" style="font-family: courier;"><tr><td><nobr>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,')<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;(<br/>
&nbsp;&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p;&nbsp;(_(__|&nbsp;&nbsp;&nbsp;((__|&nbsp;&nbsp;&nbsp;<br/>
</nobr></td></tr>
<tr><td><nobr>
&nbsp;&nbsp;(\(\<br/>
&nbsp;&nbsp;)..&nbsp;\<br/>
&nbsp;&nbsp;\Y_,&nbsp;'-.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;\/&nbsp;&nbsp;&nbsp;\&nbsp;<br/>
&nbsp;&nbsp;&nbsp;&nbsp;\\&nbsp;|\_&nbsp;&nbsp;|_<br/>
jgs&nbsp;((_/(__/_,'.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(,----'<br/>
</nobr></td></tr></table>

<!--
                       ,')
                      ( (
                       ) )
                      / /
                     / /
                  ,-'  `-.
                ,'        \
     _    , _,-'    (      )
     )`--/,)         `.   (
    /      \ (         )  /
   6),6>    ) \     )_/  /
 __(Y:.  _,'   )   /(((_/
(((_^---'`._,-'   /  ```   hjw
'''       ((( _.-'
          '''



    /\-/\
   /a a  \                  _
  =\ Y  =/-~~~~~~-,________/ )
    '^--'          _________/
      \           /
      ||  |---'\  \   
jgs  (_(__|   ((__|   



  (\(\
  ).. \
  \Y_, '-.
    )     '.
    |  \/   \ 
    \\ |\_  |_
jgs ((_/(__/_,'.
         (,----'
		 -->



<p>So this program's code is only about twice the size of Dragon World (if you don't count the pictures). Go ahead and type in this code into the file editor, and save the file as hangman.py. Then run the program by pressing F5. It might be a good idea to save the file every once in a while as you type it, so that if something happens to your computer or IDLE crashes, you won't lose everything you have typed.</p>

<!--
<p>TODO: Should also offer a primer on how to Copy and Paste.</p>

Nah, I'm going to leave that out.
-->

<h2>Sample Run</h2>
<blockquote class="samplerun">
H A N G M A N<br/>
<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;+------+<br/>
&nbsp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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
==============<br/>
<br/>
Missed letters: a<br/>
_ _ _ e _<br/>
Guess a letter.<br/>
<span class="sampleruninput">u</span><br/>
<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;+------+<br/>
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
==============<br/>
<br/>
Missed letters: a u<br/>
_ _ _ e _<br/>
Guess a letter.<br/>
<span class="sampleruninput">r</span><br/>
<br/>
<br/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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
==============<br/>
<br/>
Missed letters: a u i<br/>
o _ _ e r<br/>
Guess a letter.<br/>
<span class="sampleruninput">t</span><br/>
Yes! The secret word is "otter"! You have won!<br/>
Do you want to play again? (yes or no)<br/>
<span class="sampleruninput">no</span><br/>
</blockquote>


<h2>Source Code</h2>

<blockquote class="sourcecode"><span class='sourcecodeHeader'>hangman.py</span><br/>
<ol start=1>
<li>import random</li>
<li></li>
<li>HANGMANPICS = ['''</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
<li></li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/|\&nbsp;&nbsp;&nbsp;&nbsp; |</li>
<li>&nbsp;&nbsp;/ | \&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
<li></li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nb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p;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/ \&nbsp;&nbsp;&nbsp;&nbsp; |</li>
<li>&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''']</li>
<li></li>
<li>words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite mole monkey moose moth mouse mule newt otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()</li>
<li></li>
<li>def getRandomWord(wordList):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns a random string from the passed list of strings.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordIndex = random.randint(0, len(wordList) - 1)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return wordList[wordIndex]</li>
<li></li>
<li>def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print HANGMANPICS[len(missedLetters)]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Missed letters:',</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in missedLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_' * len(secretWord)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)): # replace blanks with correctly guessed letters</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[i] in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:i] + secretWord[i] + blanks[i+1:]</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in blanks: # show the secret word with spaces in between each letter</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
<li></li>
<li>def getGuess(alreadyGuessed):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Guess a letter.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guess = raw_input()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guess = guess.lower()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(guess) != 1:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please enter a single letter.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif guess in alreadyGuessed:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'You have already guessed that letter. Choose again.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif guess not in 'abcdefghijklmnopqrstuvwxyz':</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please enter a LETTER.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return guess</li>
<li></li>
<li>def playAgain():</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns True if the player wants to play again, otherwise it returns False.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Do you want to play again? (yes or no)'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return raw_input().lower().startswith('y')</li>
<li></li>
<li></li>
<li>print 'H A N G M A N'</li>
<li>missedLetters = ''</li>
<li>correctLetters = ''</li>
<li>secretWord = getRandomWord(words)</li>
<li>gameIsDone = False</li>
<li></li>
<li>while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Let the player type in a letter.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;guess = getGuess(missedLetters + correctLetters)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;if guess in secretWord:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correctLetters = correctLetters + guess</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if the player has won</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foundAllLetters = True</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[i] not in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foundAllLetters = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if foundAllLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Yes! The secret word is "' + secretWord + '"! You have won!'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = True</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missedLetters = missedLetters + guess</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if player has guessed too many times and lost</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(missedLetters) == len(HANGMANPICS) - 1:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = True</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Ask the player if they want to play again (but only if the game is done).</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;if gameIsDone:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if playAgain():</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missedLetters = ''</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correctLetters = ''</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord = getRandomWord(words)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</li>
<li></li>
</ol>
</blockquote>

<p>After typing in the source code (don't forget to save!) you can run this game by pressing F5. If any errors come up, be sure you typed the source code in <i>exactly</i> as it appears here. Remember that the indentation is important, and that lines will have zero, four, eight, or even twelve spaces in front of them.</p>

<h2>Designing the Program</h2>

<p>This game is a bit more complicated, so it will help if we take a moment to think about how we will put the program together. We will create a flow chart (like the flow chart at the end of the Dragon World chapter) to think about what this program will do. Of course, we don't <i>have</i> to write out a flow chart. We could just start writing code. But many times when we are writing code, we will think of new things to add or other events in the program that we didn't think of. We may have to end up changing the code we have already written, or deleting a lot of the code. That would be a waste of effort. We can save a lot of time if we think about the program before writing it.</p>

<p>This flow chart is provided as an example for what flow charts look like and how to make them. Because you only have to copy the source code from this book, you don't need to draw a flow chart before writing code. But when you make your own games, a flow chart can be very handy.</p>

<p>This flow chart will also help you learn how to design games yourself, instead of just copying the source code from this book. Your flow chart doesn't have to look exactly like this one. You may have extra boxes or fewer boxes. But as long as it <i>you</i> understand the flow chart you made, it will be helpful when you start coding.</p>

<p>First we'll start with a flow chart that only has a "Start" and an "End" box:</p>

<p class='centeredImageP'><img src='images/6_flow1.png' class='centeredImage'></p>

<p>Now let's think about what happens when the play Hangman. Well, there is a secret word (the computer will think of this) and then the other person guesses letters (the player will do this) so let's add boxes for those:</p>

<p class='centeredImageP'><img src='images/6_flow2.png' class='centeredImage'></p>

<p>The game doesn't end after the player guesses a letter. The game should check if the letter is in the secret word or not. The letter either will be there or it won't be, so we should put <i>two</i> new boxes in:</p>

<p class='centeredImageP'><img src='images/6_flow3.png' class='centeredImage'></p>

<p>If the letter was in the secret word, we should also check if the player has won. And if the letter was not in the secret word, another body part gets added and the player might have lost. We can add boxes for those cases too. We don't need an arrow from the "Letter is in secret word." box to the "Player has run out of body parts and loses." box because if you think about it, you cannot possibly lose as long as you are guessing correct letters. Also, you cannot possibly win if you are guessing wrong letters. That is why we don't have those arrows. The flow chart now looks like:</p>

<p class='centeredImageP'><img src='images/6_flow4.png' class='centeredImage'></p>

<p>After the player has won or lost, we will ask the player if they want to play again with a new secret word. If the player doesn't want to play again, the program will terminate. Otherwise, we will think of a new secret word.</p>

<p class='centeredImageP'><img src='images/6_flow5.png' class='centeredImage'></p>

<p>This flow chart looks like it is finished, but is there something we are forgetting? Oh yes! The player doesn't guess a letter just once. The player will have to keep guessing letters over and over until they win or lose. We should draw two new arrows so the flow chart shows this.</p>

<p class='centeredImageP'><img src='images/6_flow6.png' class='centeredImage'></p>

<p>What else are we forgetting? What if when the player guesses a letter, they guess a letter they have guessed before. The player should not win or lose in this case, but should be allowed to guess a different letter instead:</p>

<p class='centeredImageP'><img src='images/6_flow7.png' class='centeredImage'></p>

<p>Wait a second. How can the player figure out how well or how bad they're doing in this game? We need to remember to show them the hangman board and also the secret word (with the unguessed letters blanked out). Then the player will be able to see how many body parts of the hangman there are, or how much of the secret word they have guessed so far. This should happen each time the player guesses. We can add this box in between the "Come up with a secret word." box and the "Ask player to guess a letter." box:</p>

<p class='centeredImageP'><img src='images/6_flow8.png' class='centeredImage'></p>

<p>That looks good! We can always look at this flow chart while we are coding to remind ourselves of everything we want this program to do. The flow chart is kind of like a cake recipe or blueprints for a house. We could just start baking a cake or building a house, but without the plans we may forget to do a step. You won't really need this flow chart because you will just copy the source code given here. But when you design your own games, a flow chart can help you remember everything you need to code.</p>


<h2>Code Explanation</h2>

<blockquote class='sourcecode'>
<ol start=1>
<li>import random</li>
</ol>
</blockquote>

<p>The Hangman program is going to randomly select a secret word from a list of secret words. This means we will need the <span class='m'>random</span> module imported.</p>

<blockquote class='sourcecode'>
<ol start=3>
<li>HANGMANPICS = ['''</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
</ol>
<p><i>...the rest of the code is too big to show here...</i></p>
</blockquote>

<p>This "line" of code a simple variable assignment, but it actually stretches over several real lines in the source code. The actual "line" doesn't end until line 96. To help you understand what this code means, you should learn about multi-line strings and lists:</p>

<h2>Multi-line Strings</h2>

<p>Ordinarily when you write strings in your source code, the string has to be on one line. However, if you use three single-quotes instead of one single-quote to begin and end the string, the string can be on several lines:</p>

<p class='centeredImageP'><img src='images/6_multiline1.png' class='centeredImage'></p>

<p>If we didn't have multi-line strings, we would have to use the <span class='m'>\n</span> escape character to represent the new lines. But that can make the string hard to read in the source code:</p>

<p class='centeredImageP'><img src='images/6_multiline2.png' class='centeredImage'></p>

<p>Multi-line strings do not have to keep the same indentation to remain in the same block. Within the multi-line string, Python ignores the indentation rules it normally has for the where blocks end.

<blockquote class='sourceblurb'>def writeLetter():<br/>
&nbsp;&nbsp;&nbsp;&nbsp;# inside the def-block<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print '''Dear Alice,<br/>
How are you? Write back to me soon.<br/>
<br/>
Sincerely,</br>
&nbsp;&nbsp;Bob''' # end of the multi-line string and print statement<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'P.S. I miss you.' # still inside the def-block<br/>
</br>
writeLetter() # This is the first line outside the def-block.<br/></blockquote>

<h2>Constant Variables</h2>

<p>You may have noticed that <span class='m'>HANGMANPICS</span>'s name is in all capitals. This is the programming convention for constant variables. <span class='term'>Constants</span> are variables whose values do not change throughout the program. Although we can change <span class='m'>HANGMANPICS</span> just like any other variable, the all-caps reminds the programmer to not write code that does so.</p>

<p>Constant variables are helpful for providing descriptions for values that have a special meaning. Since the multi-string value never changes, there is no reason we couldn't copy this multi-line string each time we needed that value. The <span class='m'>HANGMANPICS</span> variable never varies. But it is much shorter to type <span class='m'>HANGMANPICS</span> than it is to type that large multi-line string.</p>

<p>Also, there are cases where typing the value by itself may not be obvious. If we set a variable <span class='m'>eggs = 72</span>, we may forget why we were setting that variable to the integer <span class='m'>72</span>. But if we define a constant variable <span class='m'>DOZEN = 12</span>, then we could set <span class='m'>eggs = DOZEN * 6</span> and by just looking at the code know that the <span class='m'>eggs</span> variable was set to six dozen.</p>

<p>Like all conventions, we don't <i>have</i> to use constant variables, or even put the names of constant variables in all capitals. But doing it this way makes it easier for other programmers to understand how these variables are used. (It even can help you if you are looking at code you wrote a long time ago.)</p>

<h2>Lists</h2>

<p>I will now tell you about a new data type called a <span class='term'>list</span>. A list value can contain several other values in it. Try typing this into the shell: <span class='m'>['apples', 'oranges', 'HELLO WORLD']</span>. This is a list value that contains three string values. Just like any other value, you can store this list in a variable. Try typing <span class='m'>spam = ['apples', 'oranges', 'HELLO WORLD']</span>, and then type <span class='m'>spam</span> to view the contents of <span class='m'>spam</span>.</p>

<p class='centeredImageP'><img src='images/6_spamlist.png' class='centeredImage'></p>

<p>Lists are a good way to store several different values into one variable. The individual values inside of a list are also called <span class='term'>items</span>. Try typing: <span class='m'>animals = ['aardvark', 'anteater', 'antelope', 'albert']</span> to store various strings into the variable <span class='m'>animals</span>. The square brackets can also be used to get an item from a list. Try typing <span class='m'>animals[0]</span>, or <span class='m'>animals[1]</span>, or <span class='m'>animals[2]</span>, or <span class='m'>animals[3]</span> into the shell to see what they evaluate to.</p>

<p class='centeredImageP'><img src='images/6_animals.png' class='centeredImage'></p>

<p>The number between the square brackets is the <span class='term'>index</span>. In Python, the first index is the number 0 instead of the number 1. So the first item in the list is at index 0, the second item is at index 1, the third item is at index 2, and so on. Lists are very good when we have to store lots and lots of values, but we don't want variables for each one. Otherwise we would have something like this:</p>

<p class='centeredImageP'><img src='images/6_animalindivid.png' class='centeredImage'></p>

<p>This makes working with all the strings as a group very hard, especially if you have hundreds or thousands (or even millions) of different strings that you want stored in a list. Using the square brackets, you can treat items in the list just like any other value. Try typing <span class='m'>animals[0] + animals[2]</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_animalsconcat.png' class='centeredImage'></p>

<p>Because <span class='m'>animals[0]</span> evaluates to the string <span class='m'>'aardvark'</span> and <span class='m'>animals[2]</span> evaluates to the string <span class='m'>'antelope'</span>, then the expression <span class='m'>animals[0] + animals[2]</span> is the same as <span class='m'>'aardvark' + 'antelope'</span>. This string concatenation evaluates to <span class='m'>'aardvarkantelope'</span>.

<p>What happens if we enter an index that is larger than the list's largest index? Try typing <span class='m'>animals[4]</span> or <span class='m'>animals[99]</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_indexerror.png' class='centeredImage'></p>

<p>If you try accessing an index that is too large, you will get an <span class='term'>index error</span>.</p>

<h2>Changing the Values of List Items with Index Assignment</h2>

<p>You can also use the square brackets to change the value of an item in a list. Try typing <span class='m'>animals[1] = 'ANTEATER'</span>, then type <span class='m'>animals</span> to view the list.</p>

<p class='centeredImageP'><img src='images/6_changelist.png' class='centeredImage'></p>

<p>The second item in the <span class='m'>animals</span> list has been overwritten with a new string.</p>


<h2>List Concatenation</h2>

<p>You can join lists together into one list with the <span class='m'>+</span> operator, just like you can join strings. When joining lists, this is known as <span class='term'>list concatenation</span>. Try typing <span class='m'>[1, 2, 3, 4] + ['apples', 'oranges'] + ['Alice', 'Bob']</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_listconcat.png' class='centeredImage'></p>

<p>Notice that lists do not have to store values of the same data types. The example above has a list with both integers and strings in it.</p>


<h2>The <span class='m'>in</span> Operator</h2>

<p>The <span class='m'>in</span> operator makes it easy to see if a value is inside a list or not. Expressions that use the <span class='m'>in</span> operator return a boolean value: <span class='m'>True</span> if the value is in the list and <span class='m'>False</span> if the value is not in the list. Try typing <span class='m'>'antelope' in animals</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_inop.png' class='centeredImage'></p>

<p>The expression <span class='m'>'antelope' in animals</span> returns <span class='m'>True</span> because the string <span class='m'>'antelope'</span> can be found in the list, <span class='m'>animals</span>. (It is located at index 2.)</p>

<p>But if we type the expression <span class='m'>'ant' in animals</span>, this will return <span class='m'>False</span> because the string <span class='m'>'ant'</span> does not exist in the list. We can try the expression <span class='m'>'ant' in ['beetle', 'wasp', 'ant']</span>, and see that it will return <span class='m'>True</span>.</p>

<p class='centeredImageP'><img src='images/6_inop2.png' class='centeredImage'></p>

<p>The <span class='m'>in</span> operator also works for strings as well as lists. You can check if one string exists in another the same way you can check if a value exists in a list. Try typing <span class='m'>'hello' in 'Alice said hello to Bob.'</span> into the shell. This expression will evaluate to <span class='m'>True</span>.</p>

<p class='centeredImageP'><img src='images/6_inop3.png' class='centeredImage'></p>




<h2>Removing Items from Lists with <span class='m'>del</span> Statements</h2>

<p>You can remove items from a list with a <span class='m'>del</span> statement. Try creating a list of numbers by typing: <span class='m'>spam = [2, 4, 6, 8, 10]</span> and then <span class='m'>del spam[1]</span>. Type <span class='m'>spam</span> to view the list's contents:</p>

<p class='centeredImageP'><img src='images/6_del1.png' class='centeredImage'></p>

<p>Notice that when you deleted the item at index 1, the item that used to be at index 2 became the new index 1. The item that used to be at index 3 moved to be the new index 2. Everything above the item that we deleted moved down one index. We can type <span class='m'>del spam[1]</span> again and again to keep deleting items from the list:</p>

<p class='centeredImageP'><img src='images/6_del2.png' class='centeredImage'></p>

<!-- Omit this for now. I think it adds needless details. And besides, I need to figure out if = is technically a keyword or an operator. (It doesn't seem to evaluate to anything, though you can use something like x = y = 42.)
<p>Just remember that <span class='m'>del</span> is a statement, not a function or an operator. It does not evaluate to any return value. (Just like </p>
-->




<h2>Lists of Lists</h2>

<p>Lists are a data type that can contain other values as items in the list. But these items can also be other lists. Let's say you have a list of groceries, a list of chores, and a list of your favorite pies. You can put all three of these lists into another list. Try typing this into the shell:</p>

<p><br/><span class='m'>groceries = ['eggs', 'milk', 'soup', 'apples', 'bread']<br/>
chores = ['clean', 'mow the lawn', 'go grocery shopping']<br/>
favoritePies = ['apple', 'frumbleberry']<br/>
listOfLists = [groceries, chores, favoritePies]<br/>
listOfLists</span></p>

<p class='centeredImageP'><img src='images/6_listoflists.png' class='centeredImage'></p>

<p>You could also type the following and get the same values for all four variables:</p>

<p><br/><span class='m'>listOfLists = [['eggs', 'milk', 'soup', 'apples', 'bread'], ['clean', 'mow the lawn', 'go grocery shopping'], ['apple', 'frumbleberry']]<br/>
groceries = listOfLists[0]<br/>
chores = listOfLists[1]<br/>
favoritePies = listOfLists[2]<br/>
listOfLists</span></p>

<p class='centeredImageP'><img src='images/6_lists2.png' class='centeredImage'></p>

<p>To get an item inside the list of lists, you would use <i>two</i> sets of square brackets like this: <span class='m'>listOfLists[1][2]</span> which would evaluate to the string <span class='m'>'go grocery shopping'</span>. This is because <span class='m'>listOfLists[1]</span> evaluates to the list <span class='m'>['clean', 'mow the lawn', 'go grocery shopping'][2]</span>. That finally evaluates to <span class='m'>'go grocery shopping'</span>.</p>

<p>Here is another example of a list of lists, along with some of the indexes that point to the items in the list of lists named <span class='m'>x</span>. The red arrows point to indexes of the inner lists themselves. The image is also flipped on its side to make it easier to read:</p>

<p class='centeredImageP'><img src='images/6_listoflistsexample.png' class='centeredImage'></p>





<h2>Code Explanation continued...</h2>

<blockquote class='sourcecode'>
<ol start=3>
<li>HANGMANPICS = ['''</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
</ol>
<p><i>...the rest of the code is too big to show here...</i></p>
</blockquote>

<p>If you look from line 3 to line 96 in the code, you will see that the value we are assigning to the variable <span class='m'>HANGMANPICS</span> is a list of multi-line strings. Each multi-line string in this list will be the picture (in ASCII art) of the hangman board. The string at <span class='m'>HANGMANPICS[0]</span> is the hangman's noose with no body parts. The string at <span class='m'>HANGMANPICS[1]</span> has just the head, <span class='m'>HANGMANPICS[2]</span> has the head and body, and so on.</p>

<blockquote class='sourcecode'>
<ol start=98>
<li>words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite mole monkey moose moth mouse mule newt otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()</li>
</ol>
</blockquote>

<p>Line 98 assigns a list to the variable <span class='m'>words</span>. This will be the list of all possible secret words in this game. The secret word will be selected from this list. All of the possible secret words are some kind of animal (so the player has some idea what the word is).</p>

<p>But the value being assigned to <span class='m'>words</span> doesn't look like a list. It does not have the <span class='m'>[</span> and <span class='m'>]</span> square brackets. But there is a special kind of function call at the end of the long string, <span class='m'>.split()</span>. This is a method on the string, and it will evaluate to a list which is then stored in <span class='m'>words</span>. Read on to find out what methods are.</p>


<h2>Methods</h2>

<p><span class='term'>Methods</span> are functions that are associated with a certain data type. They are associated much in the same way that the <span class='m'>randint()</span> function is associated with the <span class='m'>random</span> module. Integers do not happen to have any methods, but strings and lists do. To call a method, put a period and the method name with parentheses (and any arguments) after the value.</p>

<p>For example, the string data type has a <span class='m'>lower()</span> method. You cannot just call the <span class='m'>lower()</span> by itself. You must attach the method call to a specific string. Try typing <span class='m'>'Hello world!'.lower()</span> into the interactive shell:</p>

<p class='centeredImageP'><img src='images/6_methods1.png' class='centeredImage'></p>

<p>The <span class='m'>lower()</span> method returns the lowercase version of the string it is associated with. There is also an <span class='m'>upper()</span> method for strings. Try tying <span class='m'>'Hello world'.upper()</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_methods2.png' class='centeredImage'></p>

<p>Because the <span class='m'>upper()</span> method returns a string, you can call a method on <i>that</i> string as well. Try typing <span class='m'>'Hello world!'.upper().lower()</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_methods3.png' class='centeredImage'></p>

<p><span class='m'>'Hello world!'.upper()</span> evaluates to the string <span class='m'>'HELLO WORLD!'</span>, and then we call <i>that</i> string's <span class='m'>lower()</span> method. This returns the string <span class='m'>'hello world!'</span>, which is the final value in the evaluation. The order is important. <span class='m'>'Hello world!'.lower().upper()</span> is not the same as <span class='m'>'Hello world!'.upper().lower()</span>:</p>

<p class='centeredImageP'><img src='images/6_methods6.png' class='centeredImage'></p>

<p>Remember, if a string is stored in a variable, you can call a string method on that variable. Look at this example:</p>

<p class='centeredImageP'><img src='images/6_methods4.png' class='centeredImage'></p>

<p>The list data type also has methods. The <span class='m'>reverse()</span> method will reverse the items in the list. Try typing <span class='m'>spam = [1, 2, 3, 4, 5, 6, 'meow', 'woof']</span> and then <span class='m'>spam.reverse()</span> (to reverse the list), and then <span class='m'>spam</span> to view the contents of the <span class='m'>spam</span> variable.</p>

<p class='centeredImageP'><img src='images/6_methods5.png' class='centeredImage'></p>

<p>The most common list method you will use is <span class='m'>append()</span>. This method will add the value you pass as an argument to the end of the list. Try typing the following into the shell:</p>

<blockquote class="sourceblurb">
eggs = []<br/>
eggs.append('hovercraft')<br/>
eggs<br/>
eggs.append('eels')<br/>
eggs<br/>
eggs.append(42)<br/>
eggs</blockquote>

<p class='centeredImageP'><img src='images/6_listmethod.png' class='centeredImage'></p>



<h2>Code Explanation continued...</h2>

<blockquote class='sourcecode'>
<ol start=98>
<li>words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite mole monkey moose moth mouse mule newt otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()</li>
</ol>
</blockquote>

<p>As you can see, this line is just one very, very long string that has the <span class='m'>split()</span> method called on it. The <span class='m'>split()</span> method will return a list made up of the words in the string that are separated by a space. (The string is split up into a list of items.) The reason we do it this way instead of just writing out the list is that it is easier to type as one long string. Otherwise you would have to type: <span class='m'>['ant', 'baboon', 'badger',</span>... with all the quotes and commas. You can add or remove your own words to this string later if you want to change the words used in the Hangman game.</p>

<p>For an example of how the <span class='m'>split()</span> string method works, try typing <span class='m'>'My very energetic mother just served us nine pies'.split()</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_split.png' class='centeredImage'></p>

<p>The result is a list of nine strings, one string for each of the words in the original string. The spaces are dropped from the items in the list.</p>

<blockquote class='sourcecode'>
<ol start=100>
<li>def getRandomWord(wordList):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns a random string from the passed list of strings.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordIndex = random.randint(0, len(wordList) - 1)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return wordList[wordIndex]</li>
</ol>
</blockquote>

<p>Starting on line 100, we define a new functon called <span class='m'>getRandomWord()</span> which has a single parameter named <span class='m'>wordList</span>. We will call this function when we want to pick a secret word from a list of secret words. This function makes use of a new Python function named <span class='m'>len()</span>, which I will explain first.</p>

<h2>The len() Function</h2>

<p>The <span class='m'>len()</span> function takes a list as a parameter and returns the integer of how many items are in a list. Try typing <span class='m'>len(animals)</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_animallen.png' class='centeredImage'></p>

<p>The integer value returned by <span class='m'>len()</span> is like any other integer value:</p>

<p class='centeredImageP'><img src='images/6_lenadd.png' class='centeredImage'></p>

<p>The square brackets by themselves is also a list value known as the <span class='term'>empty list</span>. If you pass the empty list to the <span class='m'>len()</span> function, it returns the integer <span class='m'>0</span>, because there is nothing in that list:</p>

<p class='centeredImageP'><img src='images/6_lenempty.png' class='centeredImage'></p>


<h2>Code Explanation continued...</h2>

<blockquote class='sourcecode'>
<ol start=100>
<li>def getRandomWord(wordList):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns a random string from the passed list of strings.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordIndex = random.randint(0, len(wordList) - 1)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return wordList[wordIndex]</li>
</ol>
</blockquote>

<p>When we call <span class='m'>getRandomWord()</span>, we will pass a list of strings as the argument to the <span class='m'>wordList</span> parameter. On line 102, we will store a random index of this list in the <span class='m'>wordIndex</span> variable. We do this by calling <span class='m'>randint()</span> with two arguments. Remember that arguments in a function call are separated by commas, so the first argument is <span class='m'>0</span> and the second argument is <span class='m'>len(wordList) - 1</span>. The second argument is an expression that is first evaluated. <span class='m'>len(wordList)</span> will return the integer size of the list passed to <span class='m'>getRandomWord()</span>, minus one.</p>

<p>What this means is that if we passed <span class='m'>['apple', 'orange', grape']</span> as an argument to <span class='m'>getRandomWord()</span>, then <span class='m'>len(wordList)</span> would return the integer <span class='m'>3</span> and the expression would evaluate to <span class='m'>3 - 1</span>. That would make the second argument to the <span class='m'>randint()</span> function call the integer <span class='m'>2</span>.</p>

<p>That means that <span class='m'>wordIndex</span> would contain the return value of <span class='m'>randint(0, 2)</span>, which means <span class='m'>wordIndex</span> would equal <span class='m'>0</span>, <span class='m'>1</span>, or <span class='m'>2</span>. On line 103, we would return the element in <span class='m'>wordList</span> at the integer index stored in <span class='m'>wordIndex</span>.</p>

<p>Let's pretend we did send <span class='m'>['apple', 'orange', grape']</span> as the argument to <span class='m'>getRandomWord()</span> and <span class='m'>randint(0, 2)</span> returned the integer <span class='m'>2</span>. That would mean that line 103 would become <span class='m'>return wordList[2]</span>, which would evaluate to <span class='m'>return 'grape'</span>. In that case, the return value of our call to <span class='m'>getRandomWord()</span> would be the string <span class='m'>'grape'</span>.</p>

<p>But remember, we can pass any list of strings to <span class='m'>getRandomWord()</span>. This function will be very useful to our Hangman game when we call it.</p>

<blockquote class='sourcecode'>
<ol start=105>
<li>def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print HANGMANPICS[len(missedLetters)]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
</ol>
</blockquote>

<p>This code defines a new function named <span class='m'>displayBoard()</span>. This function has four parameters. This function will implement the code for the "Show the board and blanks to the player" box in our flow chart. Here is what each parameter means:</p>

<ul>
<li><span class='m'>HANGMANPICS</span> - This is a list of strings that will display the board as ASCII art. We will always pass the global <span class='m'>HANGMANPICS</span> variable as the argument for this parameter.</li>
<li><span class='m'>missedLetters</span> - This is a string made up of the letters the player has guessed that are not in the secret word.</li>
<li><span class='m'>correctLetters</span> - This is a string made up of the letters the player has guessed that are in the secret word.</li>
<li><span class='m'>secretWord</span> - This string is the secret word.</li>
</ul>

<p>The first <span class='m'>print</span> statement will display the board. <span class='m'>HANGMANPICS</span> will be a list of strings for each possible board. <span class='m'>HANGMANPICS[0]</span> shows an empty gallows, <span class='m'>HANGMANPICS[1]</span> shows the head (this happens when the player misses one letter), <span class='m'>HANGMANPICS[2]</span> shows a head and body (this happens when the player misses two letters), and so on until <span class='m'>HANGMANPICS[6]</span> when the full hangman is shown and the player loses.</p>

<p>We can tell how many missed guesses the player has made because that will be the number of letters in <span class='m'>missedLetters</span>. The return value of <span class='m'>len(missedLetters)</span> will be an integer of how many letters are in the string. We can use this integer as the index to the <span class='m'>HANGMANPICS</span> list. So, if <span class='m'>missedLetters</span> is <span class='m'>'aetr'</span> then <span class='m'>len('aetr')</span> will return <span class='m'>4</span> and we will display the string at <span class='m'>HANGMANPICS[4]</span>. This is what <span class='m'>HANGMANPICS[len(missedLetters)]</span> evaluates to. This line shows the proper hangman board to the player.</p>

<blockquote class='sourcecode'>
<ol start=109>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Missed letters:',</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in missedLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
</ol>
</blockquote>

<p>Line 110 is a new type of loop, called a for-loop. (Kind of like a while-loop.) Line 111 is the body of the for-loop, which is just a single line. The Python <span class='m'>range()</span> function is often used with for-loops. I will explain both in the next section.</p>

<h2>The <span class='m'>range()</span> Function</h2>

<p>The <span class='m'>range()</span> function is easy. You can call it with one or two integer arguments. When called with one argument, <span class='m'>range()</span> will return a list of integers from 0 up to (but not including) the argument. Try typing <span class='m'>range(10)</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_range1.png' class='centeredImage'></p>

<p>It's very easy to generate huge lists with the <span class='m'>range()</span> function. Try typing in <span class='m'>range(10000)</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_range2.png' class='centeredImage'></p>

<p>The list is so huge, that it won't even all fit onto the screen. But we can save the list into the variable just like any other list by typing <span class='m'>spam = range(10000)</span>

<p class='centeredImageP'><img src='images/6_range3.png' class='centeredImage'></p>

<p>If you pass two arguments to <span class='m'>range()</span>, the list of integers it returns is from the first argument up to (but not including) the second argument. Try typing <span class='m'>range(10, 20)</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_range4.png' class='centeredImage'></p>

<p>The <span class='m'>range()</span> is a very useful function, because we often use it in <span class='m'>for</span> loops (which are much like the <span class='m'>while</span> loops we have already seen).</p>

<h2><span class='m'>for</span> Loops</h2>

<p>The <span class='m'>for</span> loop is very good at looping over a list of values, instead of the <span class='m'>while</span> which loops as long as a condition is true. A <span class='m'>for</span> statement begins with the <span class='m'>for</span> keyword, followed by a variable, followed by the <span class='m'>in</span>, followed by a sequence (such as a list or string) and then a colon. Each time through the loop (that is, on each <span class='term'>iteration</span> through the loop) the variable in the <span class='m'>for</span> statement takes on the value of the next item in the list.</p>

<p>For example, you just learned that the <span class='m'>range()</span> function will return a list of integers. We will use this list as the <span class='m'>for</span> statement's list. In the shell, type <span class='m'>for i in range(10):</span> and press Enter. Nothing will happen, but the shell will indent the cursor, because it is waiting for you to type in the for-block. Type <span class='m'>print i</span> and press Enter. Then, to tell the interactive shell you are done typing in the for-block, press Enter again to enter a blank line. The shell will then execute your <span class='m'>for</span> statement and block:</p>

<p class='centeredImageP'><img src='images/6_forloop.png' class='centeredImage'></p>

<p>The <span class='m'>for</span> loop executes the code inside the for-block once for each item in the list. Each time it executes the code in the for-block, the variable <span class='m'>i</span> is assigned the next value of the next item in the list. If we used the <span class='m'>for</span> statement with the list <span class='m'>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span> instead of <span class='m'>range(10)</span>, it would have been the same since the <span class='m'>range()</span> function's return value is the same as that list:</p>

<p class='centeredImageP'><img src='images/6_forloop2.png' class='centeredImage'></p>

<p>Try typing this into the shell: <span class='m'>for thing in ['cats', 'pasta', 'programming', 'spam']:</span>, press Enter, then <span class='m'>print 'I really like ' + thing</span>, then Enter, and then Enter again to tell the shell to end the for-block. The output should look like this:</p>

<p class='centeredImageP'><img src='images/6_forloop3.png' class='centeredImage'></p>

<p>And remember, because strings are also a sequence data type, you can use them in <span class='m'>for</span> statements as well:</p>

<p class='centeredImageP'><img src='images/6_forloop4.png' class='centeredImage'></p>

<p>You can make a <span class='m'>while</span> loop that acts the same way as a <span class='m'>for</span> loop by adding extra code:</p>

<p class='centeredImageP'><img src='images/6_forloop5.png' class='centeredImage'></p>

<p>But using the <span class='m'>for</span> statement automatically does all this extra code for us and makes programming easier since we have less to type. Our Hangman game will use <span class='m'>for</span> loops so you can see how they are useful in real games.</p>

<p>One more thing about <span class='m'>for</span> loops, is that the <span class='m'>for</span> statement has the <span class='m'>in</span> keyword in it. When you type "in" in a <span class='m'>for</span> statement, Python does not treat it like the <span class='m'>in</span> operator (like you would use for <span class='m'>42 in [0, 42, 67]</span>). The "<span class='m'>in</span>" in <span class='m'>for</span> statements is just used to separate the variable and the list it gets its values from. It is just a part of the <span class='m'>for</span> statement.</p>





<h2>Code Explanation continued...</h2>

<blockquote class='sourcecode'>
<ol start=109>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Missed letters:',</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in missedLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
</ol>
</blockquote>

<p>This for-loop will display all the letters that the player has guessed that are not in the secret word. When you play Hangman on paper, you usually write down these letters off to the side so you know not to guess them again. The value of <span class='m'>letter</span> will be each letter in <span class='m'>missedLetters</span> on each iteration of the loop. Remember that a comma at the end of the <span class='m'>print</span> statement will make it print a space instead of a "newline" character, so all the missed letters will be on the same line.</p>

<p>If <span class='m'>missedLetters</span> was <span class='m'>'ajtw'</span> then this for-loop would print <span class='m'>a j t w</span>.</p>

<p>So by this point we have shown the player the hangman board and the missed letters. Now we want to show the secret word, except we want blank lines for the letters. We can use the _ underscore character for this. But we should show the letters in the secret word that the player has guessed. We can first create a string with nothing but underscores, one for each letter in the secret word. Then we can replace the blanks for each letter in <span class='m'>correctLetters</span>. So if the secret word was <span class='m'>'otter'</span> then the blanked out string would be <span class='m'>'_____'</span> (that's five _ characters in a string). If <span class='m'>correctLetters</span> was the string <span class='m'>'rt'</span> then we would want to change the blanked string to <span class='m'>'_tt_r'</span>. Here is the code that does that:</p>

<blockquote class='sourcecode'>
<ol start=114>
<li>&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_' * len(word)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)): # replace blanks with correctly guessed letters</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if word[i] in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:i] + word[i] + blanks[i+1:]</li>
</ol>
</blockquote>

<p>The <span class='m'>blanks</span> variable will be the string full of _ underscores. Line 116 uses string multiplication. Remember that the <span class='m'>*</span> operator can also be used on a string and an integer, so the expression <span class='m'>'hello' * 3</span> evaluates to <span class='m'>'hellohellohello'</span>. This will make sure that <span class='m'>blanks</span> has the same number of underscores as <span class='m'>secretWord</span> has letters.</p>

<p>Then we use a <span class='m'>for</span> loop to go through each letter in <span class='m'>secretWord</span>, and replace the underscore with the actual letter if it exists in <span class='m'>correctLetters</span>. Line 120 looks odd, it seems that we are adding the values at certain indexes in the <span class='m'>blanks</span> and <span class='m'>secretWord</span> variables. But wait a second, <span class='m'>blanks</span> and <span class='m'>secretWord</span> are strings, not lists. And the <span class='m'>len()</span> function also only takes lists as parameters, not strings. But in Python, many of the things you can do to lists you can also do to strings:</p>



<h2>Strings Act Like Lists</h2>

<p>Surprise! Strings act a lot like lists. In fact, almost all of the things you can do on lists you can also do on strings. Just think of strings as "lists" of one-letter strings. So <span class='m'>'Hello'</span> acts similar to <span class='m'>['H', 'e', 'l', 'l', 'o']</span>. (They are still different values and have different data types though.) The square brackets can also pick out individual characters from a string just like it can pick out individual items from a list. In the interactive shell, type <span class='m'>fizz = 'Hello world!'</span> and then <span class='m'>fizz[0]</span>:</p>

<p class='centeredImageP'><img src='images/6_stringlist.png' class='centeredImage'></p>

<p>You can also find out how many characters are in a string with the <span class='m'>len()</span> function. Type in <span class='m'>len(fizz)</span> into the shell:

<p class='centeredImageP'><img src='images/6_lenfizz.png' class='centeredImage'></p>

<p>However, you cannot change a character in a string or remove a character with <span class='m'>del</span> statement. This is because a list is a <span class='term'>mutable sequence</span> and a string is an <span class='term'>immutable sequence</span>. "Mutable" is another word for "changeable". The word "immutable" means "cannot be changed". A "sequence" is a series of things (like in real life, a dance sequence is a series of different dance steps done one after another). The reason strings are immutable and lists are mutable has to do with how the Python interpreter is programmed, but it isn't important for us to know in order to make games. If we want to change a string, we can create a copy of the string with slices.</p>

<p class='centeredImageP'><img src='images/6_sequences.png' class='centeredImage'></p>

<p>So remember, you can use index assignment or <span class='m'>del</span> with lists but not with strings.</p>

<h2>List Slicing and Substrings</h2>

<p><span class='term'>Slicing</span> is like indexing with multiple indexes instead of just one. Instead of putting one index in between the square brackets, we put two indexes separated by a colon. To grab the first three items from our <span class='m'>animals</span> list with <span class='m'>animals[0:3]</span>, which means "all items in animals from item 0 up to (but not including) item 3".</p>

<p class='centeredImageP'><img src='images/6_slice1.png' class='centeredImage'></p>

<p>To grab items 2 and 3 from <span class='m'>animals</span>, use the slice <span class='m'>animals[2:4]</span>. Try typing it into the shell:</p>

<p class='centeredImageP'><img src='images/6_slice2.png' class='centeredImage'></p>

<p>You can use slicing to get a part of a string (called a <span class='term'>substring</span> from a string. Try typing <span class='m'>'Hello world!'[3:8]</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_substring.png' class='centeredImage'></p>

<p>So remember that on the right side of any list or string value (or a variable that contains a list or string value), you can put square brackets to extract a single or several items from the <span class='term'>sequence</span>. ("Sequence" refers to a group of data types that include strings and lists.)</p>



<h2>Code Explanation continued...</h2>

<blockquote class='sourcecode'>
<ol start=116>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)): # replace blanks with correctly guessed letters</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[i] in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:i] + secretWord[i] + blanks[i+1:]</li>
</ol>
</blockquote>

<p>Let's pretend the value of <span class='m'>secretWord</span> is <span class='m'>'otter'</span> and the value in <span class='m'>correctLetters</span> is <span class='m'>'tr'</span>. Then <span class='m'>len(secretWord)</span> will return <span class='m'>5</span>. Then <span class='m'>range(len(secretWord))</span> becomes <span class='m'>range(5)</span>, which in turn returns the list <span class='m'>[0, 1, 2, 3, 4]</span>.</p>

<p>Because the value of <span class='m'>i</span> will take on each value in <span class='m'>[0, 1, 2, 3, 4]</span>, then the for-loop code is equivalent to this:</p>

<blockquote class='sourcecode'>
&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[0] in correctLetters:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:0] + secretWord[0] + blanks[1:]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[1] in correctLetters:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:1] + secretWord[1] + blanks[2:]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[2] in correctLetters:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:2] + secretWord[2] + blanks[3:]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[3] in correctLetters:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:3] + secretWord[3] + blanks[4:]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[4] in correctLetters:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:4] + secretWord[4] + blanks[5:]<br/>
</blockquote>

<p>(By the way, writing out the code like this instead of using a loop is called <span class='term'>loop unrolling</span>.)</p>

<p>If you are confused as to what the value of something like <span class='m'>secretWord[0]</span> or <span class='m'>blanks[3:]</span> is, then look at this picture. It shows the value of the <span class='m'>secretWord</span> and <span class='m'>blanks</span> variables, and the index for each letter in the string.</p>

<p class='centeredImageP'><img src='images/6_swap.png' class='centeredImage'></p>

<p>If we replace the list slices and the list indexes with the values that they represent, the unrolled loop code would be the same as this:</p>

<blockquote class='sourcecode'>
&nbsp;&nbsp;&nbsp;&nbsp;if 'o' in 'tr':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Condition is False, blanks == '_____'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = '' + 'o' + '____' # This line is skipped.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if 't' in 'tr':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Condition is True, blanks == '_____'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_' + 't' + '___' # This line is executed.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if 't' in 'tr':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Condition is True, blanks == '_t___'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_t' + 't' + '__' # This line is executed.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if 'e' in 'tr':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Condition is False, blanks == '_tt__'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_tt' + 'e' + '_' # This line is skipped.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if 'r' in 'tr':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Condition is True, blanks == '_tt__'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_tt_' + 'r' + '' # This line is executed.<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;# blanks now has the value '_tt_r'<br/>
</blockquote>

<p>The above three boxes of code all do the <i>same thing</i> (at least, they do when <span class='m'>secretWord</span> is <span class='m'>'otter'</span> and <span class='m'>correctLetters</span> is <span class='m'>'tr'</span>. The first box is the actual code we have in our game. The second box shows code that does the same thing except without a for-loop. The third box is the same as the second box, except we have evaluated many of the expressions in the second box.</p>

<p>The next few lines of code display the new value of <span class='m'>blanks</span> with spaces in between each letter.

<br/>

<blockquote class='sourcecode'>
<ol start=120>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in blanks: # show the secret word with spaces in between each letter</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
</ol>
</blockquote>

<p>This <span class='m'>for</span> loop will print out each character in the string <span class='m'>blanks</span>. Remember that by now, <span class='m'>blanks</span> may have some of its underscores converted to the letters in <span class='m'>secretWord</span>. The comma at the end of the <span class='m'>print</span> statement causes it to display a space instead of a newline character.</p>

<p>This is the end of the <span class='m'>displayBoard()</span> function.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=124>
<li>def getGuess(alreadyGuessed):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.</li>
</ol>
</blockquote>

<p>The <span class='m'>getGuess()</span> function has a string called <span class='m'>alreadyGuessed</span> which contains the letters the player has already guessed, and will ask the player to guess a single letter. This single letter will be the return value for this function.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=126>
<li>&nbsp;&nbsp;&nbsp;&nbsp;while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Guess a letter.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guess = raw_input()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guess = guess.lower()</li>
</ol>
</blockquote>

<p>We will use a <span class='m'>while</span> loop because we want to keep asking the player for a letter until they enter text that is a single letter they have not guessed previously. Notice that the condition for the <span class='m'>while</span> loop is simply the boolean value <span class='m'>True</span>. That means the only way execution will ever leave this loop is by executing a <span class='m'>break</span> statement (which leaves the loop) or a <span class='m'>return</span> statement (which leaves the entire function).</p>

<p>The code inside the loop asks the player to enter a letter, which is stored in the variable <span class='m'>guess</span>. If the player entered a capitalized letter, it will be converted to lowercase on line 129.</p>

<h2>elif ("Else If") Statements</h2>

<p>Take a look at the following code:</p>

<blockquote class='sourceblurb'>
if catName == 'Fuzzball':<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is fuzzy.'<br/>
else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is not very fuzzy at all.'<br/>
</blockquote>

<p>This code seems rather simple. If the <span class='m'>catName</span> variable is equal to the string <span class='m'>'Fuzzball'</span>, then the <span class='m'>if</span> statement's condition is <span class='m'>True</span> and we tell the user that her cat is fuzzy. If <span class='m'>catName</span> is anything else, then we tell the user her cat is not fuzzy.</p>

<p>But what if we wanted something else besides "fuzzy" and "not fuzzy"? We could put another <span class='m'>if</span> and <span class='m'>else</span> statement inside the first <span class='m'>else</span> block like this:</p>

<blockquote class='sourceblurb'>
if catName == 'Fuzzball':<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is fuzzy.'<br/>
else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if catName == 'Spots'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is spotted.'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is neither fuzzy nor spotted.'<br/>
</blockquote>

<p>But if we wanted more things, then the code starts to have a lot of indentation:</p>

<blockquote class='sourceblurb'>
if catName == 'Fuzzball':<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is fuzzy.'<br/>
else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;if catName == 'Spots'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is spotted.'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;else:<br/>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if catName == 'FattyKitty'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is fat.'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if catName == 'Puff'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is puffy.'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is neither fuzzy nor spotted nor fat nor puffy.'<br/>
</blockquote>

<p>Typing all those spaces means you have more chances of making a mistake with the indentation. So Python has the <span class='m'>elif</span> keyword. Using <span class='m'>elif</span>, the above code looks like this:</p>

<blockquote class='sourceblurb'>
if catName == 'Fuzzball':<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is fuzzy.'<br/>
elif catName == 'Spots'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is spotted.'<br/>
elif catName == 'FattyKitty'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is fat.'<br/>
elif catName == 'Puff'<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is puffy.'<br/>
else:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print 'Your cat is neither fuzzy nor spotted nor fat nor puffy.'<br/>
</blockquote>

<p>If the condition for the <span class='m'>if</span> statement is <span class='m'>False</span>, then the program will check the condition for the first <span class='m'>elif</span> statement (which is <span class='m'>catName == 'Spots'</span>. If that condition is <span class='m'>False</span>, then the program will check the condition of the next <span class='m'>elif</span> statement. If ALL of the conditions for the <span class='m'>if</span> and <span class='m'>elif</span> statements are <span class='m'>False</span>, then the code in the <span class='m'>else</span> block executes.</p>

<p>But if one of the <span class='m'>elif</span> conditions are <span class='m'>True</span>, the elif-block code is executed and then execution jumps down to the first line past the else-block. So <i>only one</i> of the blocks in this if-elif-else statement will be executed. You can also leave off the else-block if you don't need one, and just have an if-elif statement.</p>

<h2>Code Explanation continued...</h2>

<blockquote class='sourcecode'>
<ol start=130>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(guess) != 1:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please enter a single letter.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif guess in alreadyGuessed:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'You have already guessed that letter. Choose again.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif guess not in 'abcdefghijklmnopqrstuvwxyz':</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please enter a LETTER.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return guess</li>
</ol>
</blockquote>

<p>The <span class='m'>guess</span> variable contains the text the player typed in for their guess. We need to make sure they typed in a single lowercase letter. If they didn't, we should loop back and ask them again. The <span class='m'>if</span> statement's condition checks that the text is one and only letter. If it is not, then we execute the if-block code, and then execution jumps down past the else-block. But since there is no more code after this if-elif-else statement, execution loops back to line 126.</p>

<p>If the condition for the <span class='m'>if</span> statement is <span class='m'>False</span>, we check the <span class='m'>elif</span> statement's condition on line 132. This condition is <span class='m'>True</span> if the letter exists inside the <span class='m'>alreadyGuessed</span> variable (remember, this is a string that has every letter the player has already guessed). If this condition is <span class='m'>True</span>, then we display the error message to the player, and jump down past the else-block. But then we would be at the end of the while-block, so execution jumps back up to line 126.</p>

<p>If the condition for the <span class='m'>if</span> statement and the <span class='m'>elif</span> statement are both <span class='m'>False</span>, then we check the second <span class='m'>elif</span> statement's condition on line 134. If the player typed in a number or a funny character (making <span class='m'>guess</span> have a value like <span class='m'>'5'</span> or <span class='m'>'!'</span>), then <span class='m'>guess</span> would not exist in the string <span class='m'>'abcdefghijklmnopqrstuvwxyz'</span>. If this is the case, the <span class='m'>elif</span> statement's condition is <span class='m'>True</span> </p>

<p class='centeredImageP'><img src='images/6_elif.png' class='centeredImage'></p>

<p>Unless these three conditions are all <span class='m'>False</span>, the player will keep looping and keep being asked for a letter. But when all three of the conditions are <span class='m'>False</span>, then the else-block's <span class='m'>return</span> statement will run and we will exit this loop and function.</p>

<blockquote class='sourcecode'>
<ol start=139>
<li>def playAgain():</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns True if the player wants to play again, otherwise it returns False.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Do you want to play again? (yes or no)'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return raw_input().lower().startswith('y')</li>
</ol>
</blockquote>

<p>The <span class='m'>playAgain()</span> function has just a <span class='m'>print</span> statement and a <span class='m'>return</span> statement. The <span class='m'>return</span> statement has an expression that looks complicated, but we can break it down. Once we evaluate this expression to a value, that value will be returned from this function.</p>

<p><span class='m'>raw_input().lower().startswith('y')</span></p>

<p>This expression doesn't have any operators, but it does have a function call and two method calls. The function call is <span class='m'>raw_input()</span> and the method calls are <span class='m'>lower()</span> and <span class='m'>startswith('y')</span>. Remember that method calls are function calls that are attached by a period to the value on their left. <span class='m'>lower()</span> is attached to the return value of <span class='m'>raw_input()</span>.</p>

<p><span class='m'>raw_input()</span> returns a string of the text that the user typed in. The point of the <span class='m'>playAgain()</span> function is to let the player type in yes or no to tell our program if they want to play another round of Hangman. If the player types in YES, then the return value of <span class='m'>raw_input()</span> is the string <span class='m'>'YES'</span>. <span class='m'>'YES'.lower()</span> returns the lowercase version of the attached string. So the return value of <span class='m'>'YES'.lower()</span> is <span class='m'>'yes'</span>.</p>

<p>But there's the second method call, <span class='m'>startswith('y')</span>. This function returns <span class='m'>True</span> if the associated string begins with the string parameter between the parentheses, and <span class='m'>False</span> if it doesn't. The return value of <span class='m'>'yes'.startswith('y')</span> is <span class='m'>True</span>.</p>

<p>Now we have evaluated this expression! We can see that what this does is let the player type in a response, we lowercase the response, check if it begins with the letter "y", and then return <span class='m'>True</span> if it does and <span class='m'>False</span> if it doesn't. Whew!</p>

<p>(On a side note, there is also a <span class='m'>endswith(someString)</span> string method that will return <span class='m'>True</span> if the string ends with the string in <span class='m'>someString</span> and <span class='m'>False</span> if it doesn't.)</p>

<p>Here's a step by step look at how Python evaluates this expression if the user types in YES.</p>

<blockquote class='sourceblurb'>
return raw_input().lower().startswith('y')<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/downarrow.png"><br/>
return 'YES'.lower().startswith('y')<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/downarrow.png"><br/>
return 'yes'.startswith('y')<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/downarrow.png"><br/>
return True<br/>
</blockquote>

<p>That's all the functions we are creating for this game! <span class='m'>getRandomWord(wordList)</span> will take a list of strings passed to it as a parameter, and return one string from it. That is how we will choose a word for the player to guess.</p>

<p><span class='m'>displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</span> will show the current state of the board, including how much of the secret word the player has guessed so far and the wrong letters the player has guessed. This function needs four parameters passed to work correctly. <span class='m'>HANGMANPICS</span> is a list of strings that hold the ASCII art for each possible hangman board. <span class='m'>correctLetters</span> and <span class='m'>missedLetters</span> are strings made up of the letters that the player has guessed that are in and not in the secret word. And <span class='m'>secretWord</span> is the secret word the player is trying to guess. This function has no return value.</p>

<p><span class='m'>getGuess(alreadyGuessed)</span> takes a string of letters the player has already guessed and will keep asking the player for a letter that is a letter that he hasn't already guessed. (That is, a letter that is not in <span class='m'>alreadyGuessed</span>. This function returns the string of the acceptable letter the player guessed.</p>

<p><span class='m'>playAgain()</span> is a function that asks if the player wants to play another round of Hangman. This function returns <span class='m'>True</span> if the player does and <span class='m'>False</span> if the player doesn't.</p>

<p>We'll now start the code for the main part of the game, which will call the above functions as needed. Look back at our flow chart.</p>

<p class='centeredImageP'><img src='images/6_flow8.png' class='centeredImage'></p>

<p>We need to write code that does everything in this flow chart, and does it in order. The main part of the code starts at line 145:</p>

<blockquote class='sourcecode'>
<ol start=145>
<li>print 'H A N G M A N'</li>
<li>missedLetters = ''</li>
<li>correctLetters = ''</li>
<li>secretWord = getRandomWord(words)</li>
<li>gameIsDone = False</li>
</ol>
</blockquote>

<p>Line 145 is the first actual line that executes in our game. (Everything previous was just function definitions and a very large variable assignment for <span class='m'>HANGMANPICS</span>.) We start with a blank string for <span class='m'>missedLetters</span> and <span class='m'>correctLetters</span>. Then we call <span class='m'>getRandomWord(words)</span>, where <span class='m'>words</span> is a variable with the huge list of possible secret words we assigned on line 98. The return value of <span class='m'>getRandomWord(words)</span> is one of these words, and we save it to the <span class='m'>secretWord</span> variable. Then we also set a variable named <span class='m'>gameIsDone</span> to <span class='m'>False</span>, which we will set to <span class='m'>True</span> when we want to signal that the game is over.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=151>
<li>while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
</ol>
</blockquote>

<p>The <span class='m'>while</span> loop's condition is always <span class='m'>True</span>, so we will always loop forever until a <span class='m'>break</span> statement is run. We will execute a <span class='m'>break</span> statement when the game is over (either because the player won or the player lost).</p>

<p>Line 152 calls our <span class='m'>displayBoard()</span> function, passing it the list of hangman ASCII art pictures and the three variables we set on lines 146, 147, and 148. Program execution moves to the start of <span class='m'>displayBoard()</span> at line 105. Based on how many letters the player has correctly guessed or missed, this function displays the hangman board to the player.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=154>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Let the player type in a letter.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;guess = getGuess(missedLetters + correctLetters)</li>
</ol>
</blockquote>

<p>If you look at our flow chart, you see only one arrow going from the "Show the board and the blanks to the player." box to the "Ask a player to guess a letter." box. Since we have already written a function to get the guess from the player, let's call that function. Remember that the function needs all the letters in <span class='m'>missedLetters</span> and <span class='m'>correctLetters</span> combined, so we will pass as an argument a string that is a concatenation of both of those strings. This argument is needed by <span class='m'>getGuess()</span> because the function has code to check if the player types in a letter that they have already guessed.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=157>
<li>&nbsp;&nbsp;&nbsp;&nbsp;if guess in secretWord:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correctLetters = correctLetters + guess</li>
</ol>
</blockquote>

<p>Now let's see if the single letter in the <span class='m'>guess</span> string exists in <span class='m'>secretWord</span>. If it does, then we should concatenate the letter in <span class='m'>guess</span> to the <span class='m'>correctLetters</span> string. Next we can check if we have guessed all of the letters and won.


<br/>

<blockquote class='sourcecode'>
<ol start=160>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if the player has won</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foundAllLetters = True</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[i] not in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foundAllLetters = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</li>
</ol>
</blockquote>

<p>How do we know if the player has guessed every single letter in the secret word? Well, <span class='m'>correctLetters</span> has each letter that the player correctly guessed and <span class='m'>secretWord</span> is the secret word itself. We can't just check if <span class='m'>correctLetters == secretWord</span> because consider this situation: if <span class='m'>secretWord</span> was the string <span class='m'>'otter'</span> and <span class='m'>correctLetters</span> was the string <span class='m'>'tore'</span>, then the player has guessed each letter in the secret. The player simply guessed the letters out of order, but they still win. Even if they did guess the letters in order, <span class='m'>correctLetters</span> would be the string <span class='m'>'oter'</span> because the player can't guess the letter t more than once. The expression <span class='m'>'otter' == 'oter'</span> would evaluate to <span class='m'>False</span> even though the player won.</p>

<p>The only way we can be sure the player won is to go through each letter in the secret word and see if it exists in <span class='m'>correctLetters</span>. If, and only if, every single letter in <span class='m'>secretWord</span> exists in <span class='m'>correctLetters</span> will the player have won.</p>

<p>Note that this is different than checking if every letter in <span class='m'>correctLetters</span> is in <span class='m'>secretWord</span>. If <span class='m'>correctLetters</span> was the string <span class='m'>'ot'</span> and <span class='m'>secretWord</span> was <span class='m'>'otter'</span>, it would be true that every letter in <span class='m'>'ot'</span> is in <span class='m'>'otter'</span>, but that doesn't mean the player has guessed the secret word and won.</p>

<p>So how can we do this? We can loop through each letter in <span class='m'>secretWord</span> and if we find a letter that does not exist in <span class='m'>correctLetters</span>, we know that the player has not guessed all the letters. This is why we create a new variable named <span class='m'>foundAllLetters</span> and set it to the boolean value <span class='m'>True</span>. We start out assuming that we have found all the letters, but will change <span class='m'>foundAllLetters</span> to <span class='m'>False</span> when we find a letter in <span class='m'>secretWord</span> that is not in <span class='m'>correctLetters</span>.</p>

<p>The for loop will go through the numbers 0 up to (but not including) the length of the word. Remember that <span class='m'>range(5)</span> will evaluate to the list <span class='m'>[0, 1, 2, 3, 4, 5]</span>. So on line 162, the program executes all the code inside the for-block with the variable <span class='m'>i</span> will be set to <span class='m'>0</span>, then <span class='m'>1</span>, then <span class='m'>2</span>, then <span class='m'>3</span>, then <span class='m'>4</span>, then <span class='m'>5</span>.</p>

<p>We use <span class='m'>range(len(secretWord))</span> so that <span class='m'>i</span> can be used to access each letter in the secret word. So if the first letter in <span class='m'>secretWord</span> (which is located at <span class='m'>secretWord[0]</span>) is not in <span class='m'>correctLetters</span>, we know we can set <span class='m'>foundAllLetters</span> to <span class='m'>False</span>. Also, because we don't have to check the rest of the letters in <span class='m'>secretWord</span>, we can just break out of this loop. Otherwise, we loop back to line 163 and check the next letter.</p>

<p>If <span class='m'>foundAllLetters</span> manages to survive every single letter without being turned to <span class='m'>False</span>, then it will keep the original <span class='m'>True</span> value we gave it. Either way, the value in <span class='m'>foundAllLetters</span> is accurate by the time we get past this for loop and run line 166.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=166>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if foundAllLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Yes! The secret word is "' + secretWord + '"! You have won!'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = True</li>
</ol>
</blockquote>

<p>This is a simple check to see if we found all the letters. If we have found every letter in the secret word, we should tell the player that they have won. We will also set the <span class='m'>gameIsDone</span> variable to <span class='m'>True</span>. We will check this variable to see if we should let the player guess again or if the player is done guessing.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=169>
<li>&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
</ol>
</blockquote>

<p>This is the start of the else-block. Remember, the code in this block will execute if the condition was <span class='m'>False</span>. But which condition? To find out, point your finger at the start of the <span class='m'>else</span> keyword and move it straight up. You will see that the <span class='m'>else</span> keyword's indentation is the same as the <span class='m'>if</span> keyword's indentation on line 157. So if the condition on line 157 was <span class='m'>False</span>, then we will run the code in this else-block. Otherwise, we skip down past the else-block to line 177.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=170>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missedLetters = missedLetters + guess</li>
</ol>
</blockquote>

<p>Because the player's guessed letter was wrong, we will add it to the <span class='m'>missedLetters</span> string. This is like what we did on line 158 when the player guessed correctly.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=172>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if player has guessed too many times and lost</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(missedLetters) == len(HANGMANPICS) - 1:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = True</li>
</ol>
</blockquote>

<!--
NOTE: I've decided to leave out tuples, as this is a book about simple programming, not computer science or software engineering. I'm cutting this concept.

<p>You may have also noticed the <span class='m'>%s</span> that is in the string. This is used for <span class='term'>string interpolation</span>. The <span class='m'>%s</span> means "a string will be inserted here". To see an example of string interpolation, type this into the interactive shell: <span class='m'>'Hello %s! It is a %s and %s day today!' % ('world', 'bright', 'sunny')</span></p>

<p class='centeredImageP'><img src='images/6_strint1.png' class='centeredImage'></p>

<p class='centeredImageP'><img src='images/6_strint2.png' class='centeredImage'></p>

<p>After a string value that contains these <span class='m'>%s</span>, you have a percent sign followed by a tuple. A <span class='term'>tuple</span> is the exact same thing as a list, except for two differences. First, you type out a tuple using parentheses like ( and ) instead of a square brackets like [ and ]. This is how you tell the Python interpreter that you are making a tuple instead of a list. Second, you cannot change or delete items from a tuple.</p>

<p>Tuples are very useful when you want to have a collection of values and you don't want to let these values change.     </p>
-->

<p>Think about how we know when the player has guessed too many times. When you play Hangman on paper, this is when the drawing of the hangman is finished. We draw the hangman on the screen with print statements, based on how many letters are in <span class='m'>missedLetters</span>. Remember that each time the player guesses wrong, we add (or as a programmer would say, concatenate) the wrong letter to the string in <span class='m'>missedLetters</span>. So the length of <span class='m'>missedLetters</span> (or, in code, <span class='m'>len(missedLetters)</span>) can tell us the number of wrong guesses.</p>

<p>At what point does the player run out of guesses and lose? Well, the <span class='m'>HANGMANPICS</span> list has 7 pictures (really, they are ASCII art strings). So when <span class='m'>len(missedLetters)</span> equals <span class='m'>6</span>, we know the player has lost because the hangman picture will be finished. (Remember that <span class='m'>HANGMANPICS[0]</span> is the first item in the list, and <span class='m'>HANGMANPICS[6]</span> is the last one. This is because the index of a list with 7 items goes from 0 to 6, not 1 to 7.)</p>

<p>So why do we have <span class='m'>len(missedLetters) == len(HANGMANPICS) - 1</span> as the condition on line 173, instead of <span class='m'>len(missedLetters) == 6</span>? Pretend that we add another string to the <span class='m'>HANGMANPICS</span> list (maybe a picture of the full hangman with a tail, or a third mutant arm). Then the last picture in the list would be at <span class='m'>HANGMANPICS[7]</span>. So not only would we have to change the <span class='m'>HANGMANPICS</span> list with a new string, but we would also have to remember to change line 173 to <span class='m'>len(missedLetters) == 7</span>. This might not be a big deal for a <br/>
 program like Hangman, but when you start writing larger programs you may have to change several different lines of code all over your program just to make a <br/>
 change in the program's behavior. This way, if we want to make the game harder or easier, we just have to add or remove ASCII art strings to <span class='m'>HANGMANPICS</span> and change nothing else.</p>

<p>A second reason we user <span class='m'>len(HANGMANPICS) - 1</span> is so that when we read the code in this program later, we know why this program behaves the way it does. If you wrote <span class='m'>len(missedLetters) == 6</span> and then looked at the code two weeks later, you may wonder what is so special about the number 6. You may have forgotten that 6 is the last index in the HANGMANPICS list. Of course, you could write a comment to remind yourself, like:</p>

<blockquote class='sourcecode'>
<ol start=173>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(missedLetters) == 6: # 6 is the last index in the HANGMANPICS list</li>
</ol>
</blockquote>

<p>But it is easier to just use <span class='m'>len(HANGMANPICS) - 1</span> instead.</p>

<p>So, when the length of the <span class='m'>missedLetters</span> string is equal to <span class='m'>len(HANGMANPICS) - 1</span>, we know the player has run out of guesses and has lost the game. We print a long string telling the user what the secret word was, and then set the <span class='m'>gameIsDone</span> value to the boolean value <span class='m'>True</span>. This is how we will tell ourselves that the game is done and we should start over.</p>

<p>Remember that when we have \n in a string, that represents the newline character.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=177>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Ask the player if they want to play again (but only if the game is done).</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;if gameIsDone:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if playAgain():</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missedLetters = ''</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correctLetters = ''</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord = getRandomWord(words)</li>
</ol>
</blockquote>

<p>If the player won or lost after guessing their letter, then our code would have set the <span class='m'>gameIsDone</span> variable to <span class='m'>True</span>. If this is the case, we should ask the player if they want to play again. We already wrote the <span class='m'>playAgain()</span> function to handle getting a yes or no from the player. This function returns a boolean value of <span class='m'>True</span> if the player wants to play another game of Hangman, and <span class='m'>False</span> if they've had enough.</p>

<p>If the player does want to play again, we will reset the values in <span class='m'>missedLetters</span> and <span class='m'>correctLetters</span> to blank strings, set <span class='m'>gameIsDone</span> to <span class='m'>False</span>, and then choose a new secret word by calling <span class='m'>getRandomWord()</span> again, passing it the list of possible secret words.</p>

<p>This way, when we loop back to the beginning of the loop (on line 151) the board will be back to the start (remember we decide which hangman picture to show based on the length of <span class='m'>missedLetters</span>, which we just set as the blank string) and the game will be just as the first time we entered the loop. The only difference is we will have a new secret word, because we programmed <span class='m'>getRandomWord()</span> to return a randomly chosen word each time we call it.</p>

<p>There is a small chance that the new secret word will be the same as the old secret word, but this is just a coincidence. Let's say you flipped a coin and it came up heads, and then you flipped the coin again and it also came up heads. Both coin flips were random, it was just a coincidence that they came up the same both times. Accordingly, you may get the same word return from <span class='m'>getRandomWord()</span> twice in a row, but this is just a coincidence.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=184>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</li>
</ol>
</blockquote>

<p>If the player typed in <span class='m'>'no'</span> when asked if they wanted to play again, then they return value of the call to the <span class='m'>playAgain()</span> function would be <span class='m'>False</span> and the else-block would have executed. This else-block only has one line, a <span class='m'>break</span> statement. This causes the execution to jump to the end of the loop that was started on line 151. But because there is no more code after the loop, the program terminates.</p>

<h2>And that's it!</h2>

<p>This program was much bigger than the Dragon World program, but this program is also more sophisticated. It really helps to make a flow chart or small sketch to remember how you want everything to work. Take a look at the flow chart and try to find the lines of code that represent each block.</p>

<p>At this point, you can move on to the next chapter. But I suggest you keep reading on to find out about some ways we can improve our Hangman game.</p>

<p>After you have played Hangman a few times, you might think that six guesses aren't enough to get many of the words. We can easily give the player more guesses by adding more multi-line strings to the <span class='m'>HANGMANPICS</span> list. It's easy, just change the ] square bracket on line 96 to a ,''' comma and three quotes (see line 96 below). Then add the following:</p>

<blockquote class='sourcecode'>
<ol start=96>
<li>==============''', '''</li>
<li></li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;[O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/|\&nbsp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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''']</li>
</ol>
</blockquote>

<p>We have added two new multi-line strings to the <span class='m'>HANGMANPICS</span> list, one with the hangman's left ear drawn, and the other with both ears drawn. Because our program will tell the player they have lost when the number of guesses is the same as the number of strings in <span class='m'>HANGMANPICS</span> (minus one), this is the only change we need to make.</p>

<p>We can also change the list of words by changing the words on line 98. Instead of animals, we could have colors, shapes, or fruits:</p>

<blockquote class='sourcecode'>
<ol start=98>
<li>words = 'red orange yellow green blue indigo violet white black brown'.split()</li>
</ol>
</blockquote>

<blockquote class='sourcecode'>
<ol start=98>
<li>words = 'square triangle rectangle circle ellipse rhombus trapazoid chevron pentagon hexagon septagon octogon'.split()</li>
</ol>
</blockquote>

<blockquote class='sourcecode'>
<ol start=98>
<li>words = 'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantalope mango strawberry tomato'.split()</li>
</ol>
</blockquote>

<h2>Dictionaries</h2>

<p>With some modification, we can change our code so that our Hangman game can use all of these words as separate sets. We can tell the player which set the secret word is from (like "animal", "color", "shape", or "fruit"). This way, the player isn't guessing animals all the time.</p>

<p>To make this change, we will introduce a new data type called a dictionary. A <span class='term'>dictionary</span> is a collection of other values much like a list, but instead of accessing the items in the dictionary with an integer index, you access them with an index of any data type (but most often strings).</p>

<p>Try typing the following into the shell:</p>
<blockquote class='sourceblurb'>
stuff = {'hello':'Hello there, how are you?', 'chat':'How is the weather?', 'goodbye':'It was nice talking to you!'}
</blockquote>

<p>Those are curly braces { and }. On the keyboard they are on the same key as the square braces [ and ]. We use curly braces to type out a dictionary value in Python. The values in between them are <span class='term'>key-value pairs</span>. The keys are the things on the left of the colon and the values are on the right of the colon. You can access the values (which are like items in lists) in the dictionary by using the key (which are like indexes in lists). Try typing into the shell <span class='m'>stuff['hello']</span> and <span class='m'>stuff['chat']</span> and <span class='m'>stuff['goodbye']</span>:</p>

<p class='centeredImageP'><img src='images/6_dict.png' class='centeredImage'></p>

<p>You see, instead of putting an integer index in between the square brackets, you put a key string index. This will evaluate to the value for that key. You can get the size (that is, how many key-value pairs in the dictionary) with the <span class='m'>len()</span> function. Try typing <span class='m'>len(stuff)</span> into the shell:</p>

<p class='centeredImageP'><img src='images/6_dictlen.png' class='centeredImage'></p>

<p>The list version of this dictionary would have only the values, and look something like this:</p>
<blockquote class='sourceblurb'>
listStuff = ['Hello there, how are you?', 'How is the weather?', 'It was nice talking to you!']
</blockquote>

<p>The list doesn't have any keys, like <span class='m'>'hello'</span> and <span class='m'>'chat'</span> and <span class='m'>'goodbye'</span> in the dictionary. We have to use integer indexes <span class='m'>0</span>, <span class='m'>1</span>, and <span class='m'>2</span>.</p>

<p>Dictionaries are different from lists because they are <span class='term'>unordered</span>. The first item in a list named <span class='m'>listStuff</span> would be <span class='m'>listStuff[0]</span>. But there is no "first" item in a dictionary, because dictionaries do not have any sort of order. Try typing this into the shell:</p>

<blockquote class='sourceblurb'>
favorites1 = {'fruit':'apples', 'animal':'cats', 'number':42}<br/>
favorites2 = {'animal':'cats', 'number':42, 'fruit':'apples'}<br/>
favorites1 == favorites2<br/>
</blockquote>

<p class='centeredImageP'><img src='images/6_dictcompare.png' class='centeredImage'></p>

<p>As you can see, the expression <span class='m'>favorites1 == favorites2</span> evaluates to <span class='m'>True</span> because dictionaries are unordered, and they are considered to be the same if they have the same key-value pairs in them. Lists are ordered, so a list with the same values in them but in a different order are not the same. Try typing this into the shell:</p>

<blockquote class='sourceblurb'>
listFavs1 = ['apples', 'cats', 42]<br/>
listFavs2 = ['cats', 42, 'apples']<br/>
listFavs1 == listFavs2<br/>
</blockquote>

<p class='centeredImageP'><img src='images/6_dictcompare2.png' class='centeredImage'></p>

<p>As you can see, the two lists <span class='m'>listFavs1</span> and <span class='m'>listFavs2</span> are not considered to be the same because order matters in lists.</p>

<p>You can also use integers as the keys for dictionaries. Dictionaries can have keys of any data type, not just strings. But remember, because <span class='m'>0</span> and <span class='m'>'0'</span> are different values, they will be different keys. Try typing this into the shell:

<blockquote class='sourceblurb'>
myDict = {'0':'a string', 0:'an integer'}<br/>
myDict[0]<br/>
myDict['0']<br/>
</blockquote>

<p class='centeredImageP'><img src='images/6_dictintindex.png' class='centeredImage'></p>

<p>You might think that using a <span class='m'>for</span> loop is hard with dictionaries because they do not have integer indexes. But actually, it's easy. Try typing the following into the shell. (Here's a hint, in IDLE, you do not have to type spaces to start a new block. IDLE does it for you. To end the block, just insert a blank line by just hitting the Enter key. Or you could start a new file, type in this code, and then press F5 to run the program.)</p>

<blockquote class='sourceblurb'>
favorites = {'fruit':'apples', 'animal':'cats', 'number':42}<br/>
for i in favorites:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print i<br/>
for i in favorites:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;print favorites[i]<br/>
</blockquote>

<p class='centeredImageP'><img src='images/6_dictloop.png' class='centeredImage'></p>

<p>As you can see, if you just use a dictionary in a <span class='m'>for</span> loop, the variable <span class='m'>i</span> will take on the values of the dictionary's <i>keys</i>, not it's values. But if you have the dictionary and the key, you can get the value as we do above with <span class='m'>favorites[i]</span>. But remember that because dictionaries are unordered, you cannot predict which order the <span class='m'>for</span> loop will execute in. Above, we typed the <span class='m'>'animal'</span> key as coming before the <span class='m'>'number'</span> key, but the <span class='m'>for</span> loop printed out <span class='m'>'number'</span> before <span class='m'>'animal'</span>.</p>

<p>Dictionaries also have two useful methods, <span class='m'>keys()</span> and <span class='m'>values()</span>. These will return (ordered) lists of the key values and the value values, respectively. Try typing the following into the shell:</p>

<blockquote class='sourceblurb'>
favorites = {'fruit':'apples', 'animal':'cats', 'number':42}<br/>
favorites.keys()
favorites.values()
</blockquote>

<p class='centeredImageP'><img src='images/6_dictmethods.png' class='centeredImage'></p>

<p>Using these methods to get a list of the keys and values that are in a dictionary can be very helpful.</p>

<h2>Sets of Words for Hangman</h2>

<p>So how can we use dictionaries in our game? First, let's change the list <span class='m'>words</span> into a dictionary whose keys are strings and values are lists of strings. (Remember that the string method <span class='m'>split()</span> evaluates to a list.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=98>
<li>words = {'Colors':'red orange yellow green blue indigo violet white black brown'.split(),</li>
<li>'Shapes':'square triangle rectangle circle ellipse rhombus trapazoid chevron pentagon hexagon septagon octogon'.split(),</li>
<li>'Fruits':'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantalope mango strawberry tomato'.split(),</li>
<li>'Animals':'bat bear beaver cat cougar crab deer dog donkey duck eagle fish frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat zebra'.split()}</li>
</ol>
</blockquote>

<p>This code is put across multiple lines in the file, even though the Python interpreter thinks of it as just one "line of code". (The line of code doesn't end until the final } curly brace.)</p>

<p>Now we will have to change our <span class='m'>getRandomWord()</span> function so that it chooses a random word from a dictionary of lists of strings, instead of from a list of strings. Here is what the function originally looked like:</p>

<br/>

<blockquote class='sourcecode'>
<ol start=100>
<li>def getRandomWord(wordList):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns a random string from the passed list of strings.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordIndex = random.randint(0, len(wordList) - 1)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return wordList[wordIndex]</li>
</ol>
</blockquote>

<p>Change the code in this function so that it looks like this:</p>

<br/>

<blockquote class='sourcecode'>
<ol start=100>
<li>def getRandomWord(wordDict):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns a random string from the passed dictionary of lists of strings, and the key also.</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# First, randomly select a key from the dictionary:
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordKey = random.choice(wordDict.keys())</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Second, randomly select a word from the key's list in the dictionary:
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return [wordDict[wordKey][wordIndex], wordKey]</li>
</ol>
</blockquote>

<p>Line 100 just changes the name of the parameter to something a little more descriptive. Now instead of choosing a random word from a list of strings, first we choose a random key from the dictionary and then we choose a random word from the key's list of strings. Line 104 calls a new function in the <span class='m'>random</span> module named <span class='m'>choice()</span>. The <span class='m'>choice()</span> function has one parameter, a list. The return value of <span class='m'>choice()</span> is an item randomly selected from this list each time it is called.</p>

<p>Remember that <span class='m'>randint(a, b)</span> will return a random integer between (and including) the two integers <span class='m'>a</span> and <span class='m'>b</span> and <span class='m'>choice(a)</span> returns a random item from the list <span class='m'>a</span>. Look at these two lines of code, and figure out why they do the exact same thing:</p>

<blockquote class='sourceblurb'>
random.randint(0, 9)<br/>
random.choice(range(0, 10))<br/>
</blockquote>

<p>Line 103 (line 109 in the new code) has also been changed. Now instead of returning the string <span class='m'>wordList[wordIndex]</span>, we are returning a list with two items. The first item is <span class='m'>wordDict[wordKey][wordIndex]</span>. The second item is <span class='m'>wordKey</span>. We return a list because we actually want the <span class='m'>getRandomWord()</span> to return two values, so putting those two values in a list and returning the list is the easiest way to do this.</p>

<p><span class='m'>wordDict[wordKey][wordIndex]</span> may look kind of complicated, but it is just an expression you can evaluate one step at a time like anything else. First, imagine that <span class='m'>wordKey</span> had the value <span class='m'>'Fruits'</span> (which was chosen on line 104) and <span class='m'>wordIndex</span> has the value <span class='m'>5</span> (chosen on line 107). Here is how <span class='m'>wordDict[wordKey][wordIndex]</span> would evaluate:</p>

<blockquote class='sourceblurb'>
wordDict[wordKey][wordIndex]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/downarrow.png'><br/>
wordDict['Fruits'][5]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/downarrow.png'><br/>
['apple', 'orange', 'lemon', 'lime', 'pear', 'watermelon', 'grape', 'grapefruit', 'cherry', 'banana', 'cantalope', 'mango', 'strawberry', 'tomato'][5]<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/downarrow.png'><br/>
'watermelon'<br/>
</blockquote>

<p>In the above case, the first item in the list this function returns would be the string <span class='m'>'watermelon'</span>.</p>

<p>There are just three more changes to make to our program. The first two are on the lines that we call the <span class='m'>getRandomWord()</span> function. The function is called on lines 148 and 184 in the original program:</p>

<blockquote class='sourcecode'>
<ol start=147>
<li>correctLetters = ''</li>
<li>secretWord = getRandomWord(words)</li>
<li>gameIsDone = False</li>
</ol>
...<br/>
<ol start=182>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord = getRandomWord(words)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
</ol>
</blockquote>

<p>Because the <span class='m'>getRandomWord()</span> function now returns a list of two items instead of a string, <span class='m'>secretWord</span> will be assigned a list, not a string. We would then have to change the code as follows:</p>

<blockquote class='sourcecode'>
<ol start=147>
<li>correctLetters = ''</li>
<li>secretWord = getRandomWord(words)</li>
<li>secretKey = secretWord[1]</li>
<li>secretWord = secretWord[0]</li>
<li>gameIsDone = False</li>
</ol>
...<br/>
<ol start=182>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord = getRandomWord(words)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretKey = secretWord[1]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord = secretWord[0]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
</ol>
</blockquote>

<p>With the above changes, <span class='m'>secretWord</span> is first a list of two items. Then we add a new variable named <span class='m'>secretKey</span> and set it to the second item in <span class='m'>secretWord</span>. Then we set <span class='m'>secretWord</span> itself to the first item in the <span class='m'>secretWord</span> list. That means that <span class='m'>secretWord</span> will then be a string.</p>

<p>But there is an easier way by doing a little trick with assignment statements. Try typing the following into the shell:</p>

<blockquote class='sourceblurb'>
a, b, c = ['apples', 'cats', 42]<br/>
a<br/>
b<br/>
c<br/>
</blockquote>

<p class='centeredImageP'><img src='images/6_multiassign.png' class='centeredImage'></p>

<p>The trick is to put the same number of variables (delimited by commas) on the left side of the = sign as are in the list on the right side of the = sign. Python will automatically assign the first item's value in the list to the first variable, the second item's value to the second variable, and so on. But if you do not have the same number of variables on the left side as there are items in the list on the right side, the Python interpreter will give you an error.</p>

<p class='centeredImageP'><img src='images/6_badmultiassign.png' class='centeredImage'></p>

<p>So we should change our code in Hangman to use this trick, which will mean our program uses fewer lines of code.</p>

<br/>

<blockquote class='sourcecode'>
<ol start=147>
<li>correctLetters = ''</li>
<li>secretWord, secretKey = getRandomWord(words)</li>
<li>gameIsDone = False</li>
</ol>
...<br/>
<ol start=182>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord, secretKey = getRandomWord(words)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
</ol>
</blockquote>

<p>The last change we will make is to add a simple print statement to tell the player which set of words they are trying to guess. This way, when the player plays the game they will know if the secret word is an animal, color, shape, or fruit. Add this line of code after line 151. Here is the original code:</p>

<br/>

<blockquote class='sourcecode'>
<ol start=151>
<li>while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
</ol>
</blockquote>

<p>Add the line so your program looks like this:</p>

<br/>

<blockquote class='sourcecode'>
<ol start=151>
<li>while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'The secret word is in the set: ' + secretKey</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
</ol>
</blockquote>

<p>Now we are done with our changes. Instead of just a single list of words, the secret word will be chosen from many different lists of words. We will also tell the player which set of words the secret word is from. Try playing this new version. You can easily change the <span class='m'>words</span> dictionary on line 98 to include more sets of words.</p>

<!--Just for reference, here is the complete program with the new changes added:</p>


<blockquote class="sourcecode"><span class='sourcecodeHeader'>hangman.py (new version)</span><br/>
<ol start=1>
<li>import random</li>
<li></li>
<li>HANGMANPICS = ['''</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
<li></li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nb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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
<li></li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/|\&nbsp;&nbsp;&nbsp;&nbsp; |</li>
<li>&nbsp;&nbsp;/ | \&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''', '''</li>
<li></li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;+------+</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/|\&nbsp;&nbsp;&nbsp;&nbsp; |</li>
<li>&nbsp;&nbsp;/ | \&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;/ \&nbsp;&nbsp;&nbsp;&nbsp; |</li>
<li>&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
<li>==============''']</li>
<li></li>
<li>words = {'Colors':'red orange yellow green blue indigo violet white black brown'.split(),</li>
<li>'Shapes':'square triangle rectangle circle ellipse rhombus trapazoid chevron pentagon hexagon septagon octogon'.split(),</li>
<li>'Fruits':'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantalope mango strawberry tomato'.split(),</li>
<li>'Animals':'bat bear beaver cat cougar crab deer dog donkey duck eagle fish frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat zebra'.split()}</li>
<li></li>
<li>def getRandomWord(wordDict):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns a random string from the passed dictionary of lists of strings, and the key also.</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# First, randomly select a key from the dictionary:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordKey = random.choice(wordDict.keys())</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Second, randomly select a word from the key's list in the dictionary:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return [wordDict[wordKey][wordIndex], wordKey]</li>
<li></li>
<li>def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print HANGMANPICS[len(missedLetters)]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Missed letters:',</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in missedLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;blanks = '_' * len(secretWord)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)): # replace blanks with correctly guessed letters</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[i] in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blanks = blanks[:i] + secretWord[i] + blanks[i+1:]</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;for letter in blanks: # show the secret word with spaces in between each letter</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print letter,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print</li>
<li></li>
<li>def getGuess(alreadyGuessed):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Guess a letter.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guess = raw_input()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guess = guess.lower()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(guess) != 1:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please enter a single letter.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif guess in alreadyGuessed:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'You have already guessed that letter. Choose again.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif guess not in 'abcdefghijklmnopqrstuvwxyz':</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please enter a LETTER.'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return guess</li>
<li></li>
<li>def playAgain():</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# This function returns True if the player wants to play again, otherwise it returns False.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'Do you want to play again? (yes or no)'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;return raw_input().lower().startswith('y')</li>
<li></li>
<li></li>
<li>print 'H A N G M A N'</li>
<li>missedLetters = ''</li>
<li>correctLetters = ''</li>
<li>secretWord, secretKey = getRandomWord(words)</li>
<li>gameIsDone = False</li>
<li></li>
<li>while True:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;print 'The secret word is in the set: ' + secretKey</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Let the player type in a letter.</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;guess = getGuess(missedLetters + correctLetters)</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;if guess in secretWord:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correctLetters = correctLetters + guess</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if the player has won</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foundAllLetters = True</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(secretWord)):</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if secretWord[i] not in correctLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foundAllLetters = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if foundAllLetters:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Yes! The secret word is "' + secretWord + '"! You have won!'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = True</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missedLetters = missedLetters + guess</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if player has guessed too many times and lost</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(missedLetters) == len(HANGMANPICS) - 1:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"'</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = True</li>
<li></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;# Ask the player if they want to play again (but only if the game is done).</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;if gameIsDone:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if playAgain():</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missedLetters = ''</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correctLetters = ''</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameIsDone = False</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secretWord, secretKey = getRandomWord(words)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</li>
<li></li>
</ol>
</blockquote>
-->
<p>We're done with Hangman. Let's move on to our next game, Tic Tac Toe!



<blockquote class='inthischapter'><h3>Things Covered In This Chapter:</h3>
<ul>
<li>Designing our game by drawing a flow chart before programming.</li>
<li>ASCII Art</li>
<li>Multi-line Strings</li>
<li>Lists</li>
<li>List indexes</li>
<li>Index assignment</li>
<li>List concatenation</li>
<li>The <span class='m'>in</span> operator</li>
<li>The <span class='m'>del</span> operator</li>
<li>Methods</li>
<li>The <span class='m'>append()</span> list method</li>
<li>The <span class='m'>lower()</span> and <span class='m'>upper()</span> string methods</li>
<li>The <span class='m'>reverse()</span> list method</li>
<li>The <span class='m'>split()</span> list method</li>
<li>The <span class='m'>len()</span> function</li>
<li>Empty lists</li>
<li>The <span class='m'>range()</span> function</li>
<li><span class='m'>for</span> loops</li>
<li>Strings act like lists</li>
<li>Mutable sequences (lists) and immutable sequences (strings)</li>
<li>List slicing and substrings</li>
<li><span class='m'>elif</span> statements</li>
<li>The <span class='m'>startswith(someString)</span> and <span class='m'>endswith(someString)</span> string methods</li>
<li>The dictionary data type (which is unordered, unlike list data type which is ordered)</li>
<li>key-value pairs</li>
<li>The <span class='m'>keys()</span> and <span class='m'>values()</span> dictionary methods</li>
<li>Multiple variable assignment, such as <span class='m'>a, b, c = [1, 2, 3]</span></li>
</ul>
</blockquote>

</body>
</html>